package cn.kongwg.servlet;

import cn.kongwg.bean.Cart;
import cn.kongwg.bean.User;
import cn.kongwg.dao.BaseDao;
import cn.kongwg.service.OrderService;
import cn.kongwg.service.impl.OrderServiceImpl;
import cn.kongwg.utils.JdbcUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author kongwg
 * @create 2021-02-01 19:39
 */
@WebServlet("/orderServlet")
public class OrderServlet extends BaseServlet {
    private OrderService orderService = new OrderServiceImpl();

    protected void createOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Cart cart = (Cart) req.getSession().getAttribute("cart");

        User user = (User) req.getSession().getAttribute("user");

        if (user == null) {
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req, resp);
            return;
        }

        String order = orderService.createOrder(cart, user.getId());

        req.getSession().setAttribute("orderId", order);

        req.getRequestDispatcher("/pages/cart/checkout.jsp").forward(req, resp);
    }
}
